import java.util.Arrays;

public class MyArrayList implements IList{
    public int[] elem;
    public int useSize;
    public static final int DEFAULT_CAPACITY = 5;
    public MyArrayList(){
        elem = new int[DEFAULT_CAPACITY];
    }

    @Override
    public void display() {
        for (int i = 0; i < useSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }

    @Override
    public void add(int data) {
//        尾插入
//        判断是否满了，满了需要扩容
        if (isFull()){
            elem = copyOf();
        }
        elem[useSize] = data;
        useSize++;
    }

    private int[] copyOf(){
        elem = Arrays.copyOf(elem,2*elem.length);
        return elem;
    }

    @Override
    public boolean isFull() {
        return useSize == elem.length;
    }

    @Override
    public boolean isEmpty() {
        return useSize==0;
    }

    @Override
    public void add(int pos, int data) {
//        pos位置的判断
//        给pos位置添加
        for (int i = useSize-1; i >= pos; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        useSize++;
    }

    private void checkPos(int pos){
        if (pos < 0 || pos > useSize){
            throw new PosException("pos位置为" + pos);
        }
    }

//    查找当前元素是否存在
    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if (elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

//    查找当前元素的下标
    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if (elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

//    获取pos位置的值
    @Override
    public int get(int pos) {
//        1.判断pos是否合法
        checkPosOfGet(pos);
//        2.是否为空
        if (isEmpty()){
            throw new EmptyException("数组为空");
        }
        return elem[pos];
    }

    private void checkPosOfGet(int pos){
//        插入就可以等于，但是这个等于就不合法
        if (pos < 0 || pos >= useSize){
            throw new PosException("pos不合法："+pos);
        }
    }


    /*
    更新pos位置的值为value
    */
    @Override
    public void set(int pos, int value) {
//        pos位置是否合法
        checkPosOfGet(pos);
        if (isEmpty()){
            throw new EmptyException("顺序表为空");
        }
        this.elem[pos] = value;
    }

    /*
    * 删除数字
    */
    @Override
    public void remove(int toRemove) {
        if (isEmpty()){
            throw new EmptyException("顺序表为空");
        }

        int index = indexOf(toRemove);

        for (int i = index; i < useSize-1; i++) {
            elem[i] = elem[i+1];
        }
        useSize--;
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public void clear() {
//        useSize = 0;
//        如果是引用类型，应该每一位都置空
//        for (int i = 0; i < useSize; i++) {
//            elem[i] = null;
//        }
        useSize = 0 ;
    }



}
